package cn.lyz.authority.security;

import cn.lyz.authority.system.dto.LoginUser;
import cn.lyz.authority.system.entity.SysUser;
import cn.lyz.authority.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Objects;

@Service
public class UserDetailsServiceImp implements UserDetailsService {

    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName,username);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if(Objects.isNull(sysUser)){
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        return new LoginUser(sysUser);
    }
}
